package com.ada.ielasticsearch;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws UnknownHostException {
		App client = new App();
		client.init();
		for (int i = 0; i < 10; i++) {
			client.createIndex();
		}
		client.search();
		client.close();
	}

	public void search() {
		SearchResponse response = client.prepareSearch("users").setTypes("user")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.termQuery("name", "fox")) // Query
				// .setFilter(FilterBuilders.rangeFilter("age").from(20).to(30))
				// // Filter
				.setFrom(0).setSize(60).setExplain(true).execute().actionGet();
		SearchHits hits = response.getHits();
		System.out.println(hits.getTotalHits());
		for (int i = 0; i < hits.getHits().length; i++) {
			System.out.println(hits.getHits()[i].getSourceAsString());
		}
	}

	public void close() {
		client.close();
	}

	private static void s() throws UnknownHostException {
		InetAddress localhost = InetAddress.getByName("localhost");
		// TODO Auto-generated method stub
		// InetSocketTransportAddress
		TransportClient client = TransportClient.builder().build()
				.addTransportAddress(new InetSocketTransportAddress(localhost, 9300));
		List<DiscoveryNode> ns = client.listedNodes();
		for (DiscoveryNode discoveryNode : ns) {
			System.out.println(discoveryNode);
		}
	}

	private Client client;

	public void init() throws UnknownHostException {
		InetAddress localhost = InetAddress.getByName("localhost");
		// TODO Auto-generated method stub
		// InetSocketTransportAddress
		client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(localhost, 9300));
	}

	/**
	 * index
	 */
	public void createIndex() {
		for (int i = 0; i < 10; i++) {
			System.out.println("*****************" + i + "**************");
			User user = new User();
			user.setId(new Long(i));
			user.setName("我们将要部署一个非常简单的应用--在一个部门里的雇员--这样我们可以把注意力放在功能而不是氧立得复杂性上。总而言之，这篇博文是为了帮助人们开始ElasticSearch入门。 " + i);
			user.setAge(100);
			IndexResponse back = client.prepareIndex("users", "user").setId(""+i).setSource(generateJson(user)).execute()
					.actionGet();
			System.out.println(back);
		}
	}

	/**
	 * 转换成json对象
	 *
	 * @param user
	 * @return
	 */
	private String generateJson(User user) {
		String json = "";
		try {
			XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject();
			contentBuilder.field("id", user.getId() + "");
			contentBuilder.field("name", user.getName());
			contentBuilder.field("age", user.getAge() + "");
			json = contentBuilder.endObject().string();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
}
